package com.example.demo.ImgTest;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.image.v2.ImageClient;
import com.huaweicloud.sdk.image.v2.model.ImageMediaTaggingReq;
import com.huaweicloud.sdk.image.v2.model.RunImageMediaTaggingRequest;
import com.huaweicloud.sdk.image.v2.model.RunImageMediaTaggingResponse;
import com.huaweicloud.sdk.image.v2.region.ImageRegion;

/**
 * 媒资图像标签(分类)--相对准确
 * <p>
 * 对用户传入的图像可以返回图像中的物体名称、所属类别及置信度信息。
 */
public class HuaWeiImgA {
    public static void main(String[] args) {
        //此处需要输入您的AK/SK信息
        String ak = "D4V7FPPVAS2W3YRJSPYF";
        String sk = "dKmJyVJtxe4zn3mfvVVgTpSqtQB36MADe8H4reSz";

        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);

        //此处替换为您开通服务的区域
        ImageClient client = ImageClient.newBuilder()
                .withCredential(auth)
                .withRegion(ImageRegion.valueOf("cn-north-4"))
                .build();
        RunImageMediaTaggingRequest request = new RunImageMediaTaggingRequest();
        ImageMediaTaggingReq body = new ImageMediaTaggingReq();
        body.withThreshold(40f);
        body.withLanguage("zh");
        // 默认10个结果,最多设置50
        body.withLimit(1);
        // 此处替换为公网可以访问的图片地址
//        body.withUrl("http://wnd.agri114.cn/productimg/zbdq/xiaomai.jpg");
        body.withUrl("http://114.115.166.192:9910/api/1.0/assets/BUSINESS/system/2024/170531125488661724239b67d991962cf0a80.jpg?preview=true");
//        body.withUrl("https://wnd.agri114.cn/file/cropguard/img/classifyPest/蚂蚱.png");
//        body.withUrl("https://img.zcool.cn/community/01346958ff6ef0a801214550ad655e.jpg@1280w_1l_2o_100sh.jpg");
//        body.withUrl("http://114.115.166.192:9910/api/1.0/assets/BUSINESS/system/2024/1705029849758091e4317818da0108491bc65.jpg?preview=true");
        request.withBody(body);
        try {
            RunImageMediaTaggingResponse response = client.runImageMediaTagging(request);
            System.out.println(response.toString());
        } catch (ConnectionException | RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
